﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MapEditor
{
    public class FrameRate
    {
        private static int lastTick;
        private static int lastFrameRate;
        private static int frameRate;

        private static int deltaTime;
        private static int lastTime;

        /// <summary>
        /// calcule le framerate avec System.Environment.TickCount.
        /// </summary>
        public static int CalculateFrameRate()
        {
            if (System.Environment.TickCount - lastTick >= 1000)
            {
                lastFrameRate = frameRate;
                frameRate = 0;
                lastTick = System.Environment.TickCount;
            }
            frameRate++;
            return lastFrameRate;
        }

        /// <summary>
        /// bride le framerate avec System.Environment.TickCount à 60FPS.
        /// </summary>
        public static void limitFrameRate()
        {
            int time = System.Environment.TickCount;
            deltaTime = time - lastTime;
            lastTime = time;
            if (deltaTime < 32)
                Thread.Sleep(32 - deltaTime);
        }

    }
}
